package com.cskaoyan.service;

import com.cskaoyan.bean.bo.wjjBO.WXCouponExchangeBO;
import com.cskaoyan.bean.bo.wjjBO.WXCouponReceiveBO;
import com.cskaoyan.bean.marketBean.MarketCoupon;
import com.cskaoyan.bean.marketBean.MarketCouponUser;
import com.cskaoyan.mapper.marketMapper.CouponMapper_yjc;
import com.cskaoyan.mapper.marketMapper.UserMapper_yjc;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CouponServiceimpl_yjc implements CouponService_yjc {

    @Autowired
    CouponMapper_yjc couponMapper_yjc;

    @Override
    public PageInfo queryAllCoupons() {
        PageHelper.startPage(1, 10);
        List list = couponMapper_yjc.queryAllCoupons();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public PageInfo queryMyCoupons() {
        PageHelper.startPage(1, 10);
        List list = couponMapper_yjc.queryMyCoupons();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public PageInfo selectCoupon() {
        PageHelper.startPage(1, 10);
        List list = couponMapper_yjc.selectCoupon();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
        @Override
    public int receiveCoupon(WXCouponReceiveBO couponReceiveBO) {

        MarketCoupon coupon = new MarketCoupon();
        coupon.setId(couponReceiveBO.getCouponId());
        MarketCoupon marketCoupon = couponMapper_yjc.selectMarketCouponByCouponId(coupon);

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(couponReceiveBO.getUserId());
        marketCouponUser.setCouponId(couponReceiveBO.getCouponId());
        marketCouponUser.setStatus(marketCoupon.getStatus());
        marketCouponUser.setStartTime(marketCoupon.getStartTime());
        marketCouponUser.setEndTime(marketCoupon.getEndTime());
        marketCouponUser.setAddTime(marketCoupon.getAddTime());
        marketCouponUser.setUpdateTime(marketCoupon.getUpdateTime());
        marketCouponUser.setDeleted(marketCoupon.getDeleted());

        List<MarketCouponUser> couponUserList = couponMapper_yjc.selectMarketCouponUserByUserIdCouponId(marketCouponUser);

        if (couponUserList.size() >= marketCoupon.getLimit() && marketCoupon.getLimit() != 0) {
            return 401;
        }
        int code = couponMapper_yjc.insertCouponUser(marketCouponUser);
        return 200;
    }

    @Override
    public int exchangeCoupon(WXCouponExchangeBO wxCouponExchangeBO) {
        MarketCoupon coupon = new MarketCoupon();
        coupon.setCode(wxCouponExchangeBO.getCode());
        MarketCoupon marketCoupon = couponMapper_yjc.selectMarketCouponByCode(coupon);

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(wxCouponExchangeBO.getUserId());
        int i =couponMapper_yjc.selectmy(coupon);
        marketCouponUser.setCouponId(i);
        marketCouponUser.setStatus(marketCoupon.getStatus());
        marketCouponUser.setStartTime(marketCoupon.getStartTime());
        marketCouponUser.setEndTime(marketCoupon.getEndTime());
        marketCouponUser.setAddTime(marketCoupon.getAddTime());
        marketCouponUser.setUpdateTime(marketCoupon.getUpdateTime());
        marketCouponUser.setDeleted(marketCoupon.getDeleted());

        List<MarketCouponUser> couponUserList = couponMapper_yjc.selectMarketCouponUserByUserIdCouponId(marketCouponUser);

        if (couponUserList.size() >= marketCoupon.getLimit() && marketCoupon.getLimit() != 0) {
            return 401;
        }
        couponMapper_yjc.insertCouponUser(marketCouponUser);
        return 200;
    }
}
